//
//  MPLoginViewController.m
//  WeChatQQStaticLibDemo
//
//  Created by mac on 17/1/4.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "MPLoginViewController.h"
#import "WeChatQQStaticLibHeader.h"

@interface MPLoginViewController ()
{
    NSString * m_password;
    UIColor * m_color;
}
@end

@implementation MPLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"公众号登陆";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(backBtnAction)];
    
    [self.m_btnSaveAndJump setBackgroundColor:m_color];
    
    UINavigationBar *bar = [UINavigationBar appearance];
    bar.barTintColor = m_color;
    bar.tintColor = [UIColor whiteColor];
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    _m_btn_password.layer.borderWidth = 0.8;
    _m_btn_password.layer.cornerRadius = 3;
    _m_btn_password.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    [[AsyncMPLoginSimulationService stander]asyncGetPassword:^(NSString *qrcodeUrl, NSString *password) {
        if (qrcodeUrl != nil && password != nil) {
            m_password = password;
            GET_CACHE_IMAGE_HTTP(qrcodeUrl, _m_imgQRCode);
            [_m_btn_password setTitle:password forState:UIControlStateNormal];
            
            [[AsyncMPLoginSimulationService stander]asyncCheckPassword:^(MP_LOGIN_RESULT result) {
                if (result == MLR_WAIT) {
//                    SHOW_HUD_MESSAGE(@"请发送密码!");
                }
                else if(result == MLR_SUCCESS){
                    [self backBtnAction];
                }
            }];
        }
    }];
}

- (void)backBtnAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)saveImageToPhotos:(UIImage*)savedImage
{
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    NSString *msg = nil ;
    if(error != NULL){
        msg = @"保存失败!";
        NSLog(@"%@",msg);
        return;
    } else {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]){
            NSURL * url = [[NSURL alloc]initWithString:@"weixin://scanqrcode/"];
            [[UIApplication sharedApplication ]openURL:url];
        }
        else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"wechat://"]]){
            NSURL * url = [[NSURL alloc]initWithString:@"weixin://scanqrcode/"];
            [[UIApplication sharedApplication ]openURL:url];
        }
        else{
            NSLog(@"保存成功!但本机没有安装微信!");
        }
    }
}

- (IBAction)onSaveQRCodeJump2WeChatScanQRCode:(id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = m_password;
    
    UIImage * image = self.m_imgQRCode.image;
    if (image) {
        [self saveImageToPhotos:image];
    }
}

- (IBAction)onCopyPassword:(id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = m_password;
}

- (void)updateSaveBtnColor:(UIColor *)color{
    m_color = color;
    
}
@end
